package com.fengmi.service.impl;

import com.fengmi.dto.EntityDTO;
import com.fengmi.entity.MallGoodsCat;
import com.fengmi.mapper.MallGoodsCatMapper;
import com.fengmi.service.MallGoodsCatService;
import com.fengmi.vo.CatCascaderVO;
import com.fengmi.vo.InfoResult;
import com.fengmi.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Author: 施俊豪
 * Date: 2022/1/22 22:43
 * Note:
 */
@Service
public class MallGoodsCatServiceImpl implements MallGoodsCatService {

    @Autowired(required = false)
    private MallGoodsCatMapper mallGoodsCatMapper;

    /**
     * @Note : 分页查询商品类别和商品模板
     * @Date : 2022-01-22 22:43:25
     * @Author : 清酒吖
    */
    @Override
    public PageResult<MallGoodsCat> findCatAndTemp(EntityDTO<Long> entityDTO) {
        //分页
        PageHelper.startPage(entityDTO.getPageNum(),entityDTO.getPageSize());
        //两表连接查询商品的类别和商品的模板
        List<MallGoodsCat> catList = mallGoodsCatMapper.findGoodsCatAndTemplate(entityDTO.getWhere());
        // 分页结果
        PageInfo<MallGoodsCat> pageInfo = new PageInfo<>(catList);
        // 将分页结果映射给pageResult
        PageResult<MallGoodsCat> pageResult = new PageResult<>();
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setPageList(pageInfo.getList());
        pageResult.setPages(pageInfo.getPages());
        return pageResult;
    }

    /**
     * @Note : 条件查询分类
     * @Date : 2022-01-24 00:51:28
     * @Author : 清酒吖
    */
    @Override
    public InfoResult<List<MallGoodsCat>> findCats(Long id) {
        List<MallGoodsCat> catList = mallGoodsCatMapper.findCats(id);
        return new InfoResult<List<MallGoodsCat>>(true,"查询成功!!!",catList);
    }

    /**
     * @Note : 添加商品分类
     * @Date : 2022-01-24 20:57:36
     * @Author : 清酒吖
    */
    @Override
    public InfoResult insertCat(MallGoodsCat cat) {
        if(cat == null || cat.getParentId() == null){
            return new InfoResult(false,"参数错误!!!","");
        }
        if(StringUtils.isEmpty(cat.getName())){
            return new InfoResult(false, "参数错误!!!","");
        }
        Integer row = mallGoodsCatMapper.insertCat(cat);
        return new InfoResult(true,"添加成功!!!",row);
    }

    /**
     * @Note : 根据id查询商品分类
     * @Date : 2022-01-24 21:48:21
     * @Author : 清酒吖
    */
    @Override
    public InfoResult findCatById(Long id) {
        MallGoodsCat catById = mallGoodsCatMapper.findCatById(id);
        return new InfoResult(true,"查询成功!!!",catById);
    }

    /**
     * @Note : 修改商品分类
     * @Date : 2022-01-24 21:48:39
     * @Author : 清酒吖
    */
    @Override
    public InfoResult updateCat(MallGoodsCat cat) {
        if(cat == null || cat.getParentId() == null){
            return new InfoResult(false, "参数错误!!!","");
        }
        Integer row = mallGoodsCatMapper.updateCat(cat);
        return new InfoResult(true, "修改成功!!!",row);
    }

    /**
     * @Note : 级联查询商品分类
     * @Date : 2022-01-25 15:27:00
     * @Author : 清酒吖
    */
    @Override
    public InfoResult<List<CatCascaderVO>>  findCatCascader() {
        List<CatCascaderVO> catCascader = mallGoodsCatMapper.findCatCascader();
        return new InfoResult(true,"查询成功!!!",catCascader);
    }
}
